@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/course/list">课程管理</a></li>
                <li class="active">课程列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline" action="" method="get">
                    <a href="/course/create"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>
                    <div class="form-group">
                        <input type="text" class="form-control" name="title" placeholder="课程标题" value="{{$title}}">
                        <input type="hidden" class="form-control" name="is_live" value="{{$is_live?1:''}}" />
                    </div>
                    <button type="submit" class="btn btn-default">筛选</button>
                </form>
            </div>
            <ul class="nav nav-pills">
                <li role="presentation" class="{{$is_live?'':'active'}} commonCourse"><a href="/course/list">课程</a></li>
                <li role="presentation" class="{{$is_live?'active':''}}"><a class="liveCourse" href="/course/list?is_live=1">直播课</a></li>
            </ul>
            <div class="row not_live_list" style="">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">课程列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>课程名称</th>
                                <th>排序值</th>
                                <th>是否付费</th>
                                <th>学院</th>
                                <th>导师</th>
                                <th>集数</th>
                                <th>编辑时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($courseClass as $class)
                                <tr id="courseClass_{{$class->id}}">
                                    <td>{{$class->id}}</td>
                                    <td>{{$class->title}}</td>
                                    <td>{{$class->orderby}}</td>
                                    <td>{{$class->is_free==0?'免费':$class->price}}</td>
                                    @if($class->courseType)
                                        <td>{{$class->courseType->title}}</td>
                                    @else
                                        <td></td>
                                    @endif
                                    @if($class->author)
                                        <td>{{$class->author->name}}</td>
                                    @else
                                        <td></td>
                                    @endif
                                    <td>{{$class->getCourseCount()}}</td>
                                    <td>{{$class->updated_at}}</td>

                                    <td class="text-justify" style="width:30%;">
                                        <a class="btn btn-info btn-xs" href='/course/detail?id={{$class->id}}'>详情</a>
                                        <a class="btn btn-success btn-xs" href="/course/edit?id={{$class->id}}">编辑</a>
                                        <button class="btn btn-danger btn-xs" data-id="{{$class->id}}" onclick="courseDelete(this)">删除</button><button class="btn btn-info btn-xs" data-id="{{$class->id}}" data-state="{{$class->state}}" onclick="courseClassState(this)">{{$class->state?'下架':'上架'}}</button>
                                        <a class="btn btn-success btn-xs" href="/course/is_new?data={{$class->is_new}}&&id={{$class->id}}">{{$class->is_new ?"已选中":"新课速递"}}</a>
                                        <a class="btn btn-primary btn-xs" href="/courseMessage/push/{{$class->id}}">消息推送</a>
                                        <button data-page="1" data-id="{{$class->id}}" class="btn btn-success btn-xs" onclick="courseLiveList(this);">直播课列表</button>
                                        <a data-page="1" href="/course/collect/{{$class->id}}" class="btn btn-success btn-xs">收藏用户统计</a>
                                        <button data-state="{{$class->register_free_watch}}" data-id="{{$class->id}}" class="btn btn-info btn-xs" onclick="courseRegisterFree(this);">设置注册可看</button>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$courseClass->appends(['title'=>$title])->render()}}
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">直播列表</h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">

        function pushMessage(obj){
            var pushCount = $(obj).attr('data-push');
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id ,_token:_token};
            var object = obj;
            if(pushCount >0){
                layer.confirm('已推送，确定再次推送？', {
                    btn: ['是', '否'],
                }, function(index){
                    $.ajax({
                        url:'/course/push',
                        data:data,
                        type:'POST',
                        dataType:'json',
                        success:function(res){
                            layer.alert(res.message);
                            if(res.code == 0){

                            }
                        }
                    });
                    layer.close(index);
                    return true;
                }, function(index){
                    layer.close(index);
                });
            }else{
                $.ajax({
                    url:'/course/push',
                    data:data,
                    type:'POST',
                    dataType:'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).text('已推送');
                            $(object).attr('data-id',pushCount+1);
                        }
                    }
                });
            }
        }
        function courseClassState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '确定下架吗？';
                var newState = 0;
                var name = '上架'
            }else{
                var notice = '确定上架吗？';
                var newState = 1;
                var name = '下架';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/courseClass/changeState',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }
        function courseDelete(obj){
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id,_token:_token};
            layer.confirm('确定删除吗？', {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/courseClass/delete',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $("#courseClass_"+id).remove();
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });

        }

        function courseLiveList(obj){
            var id = $(obj).attr('data-id');
            var page = $(obj).attr('data-page');
            var name  = $('#course_name').val();
            var data = {id:id,page:page,name:name};
            $.ajax({
                url:'/course/live/list',
                data:data,
                type:'GET',
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        console.log(res);
                        $('#myModal .modal-body').html(res.body);
                        $('#myModal').modal('show');
                    }
                }
            });
        }
        function courseRegisterFree(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            var data = {id:id,_token:'{{csrf_token()}}'};
            if(state == 1){
                var notice = '取消注册可看？';
                var newState = 0;
                var name = '设置注册可看'
            }else{
                var notice = '设置注册可看？';
                var newState = 1;
                var name = '取消注册可看';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,free_watch:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/course/free/watch',
                    data:data,
                    type:'POST',
                    dataType:'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }
                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });

        }

        function liveCourse(obj){
            var object = obj;
            if($(obj).hasClass('liveCourse')){
                $('.live_list').css('display','block');
                $('.not_live_list').css('display','none');
                $("input[name='is_live']").val(1);
                $(obj).parent().addClass('active');
                $('.commonCourse').removeClass('active');
            }else{
                $('.not_live_list').css('display','block');
                $('.live_list').css('display','none');
                $("input[name='is_live']").val(0);
                $(obj).parent().addClass('active');
                $('.liveCourse').parent().removeClass('active');
            }
        }
    </script>
@endsection